<?php
namespace Ucmaim\Controller\Home;


class ApiController extends CommonController {

    public function VerifyCode(){
        $config =    array(
            'fontSize' => 80,
            'useCurve' => false,
            'length'   => 4,     // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    public function CheckVerifyCode(){
        $img_valid = I('post.img_valid');
        $this->ajaxReturn( $this->chkVerifyCode($img_valid, false) );
    }

    // 注册时检验手机号码是否存在
    public function CheckMobile(){
        $tel = trim(I('tel'));
        if( ! preg_match($this->mobile_preg, $tel) ){
            $code = 1;
        }else{
            $code = $this->isExistMobile($tel) ? 2 : 0;
        }
        $result['code'] = $code;
        $this->ajaxReturn($result);
    }

    // 手机号码存在 - 返回通过
    public function getMobile(){
        $tel = trim(I('tel'));
        if( ! preg_match($this->mobile_preg, $tel) ){
            $r = false;
        }else{
            $r = $this->isExistMobile($tel) ? true : false;
        }
        $this->ajaxReturn($r);
    }

    public function isOldPass(){
        $pass = trim(I('post.pass'));
        $this->_chkUserPass($pass) ? $this->ajaxReturn(true) : $this->ajaxReturn(false);
    }

    public function isDifferentFromOldPass(){
        $pass = trim(I('post.pass'));
        $this->_chkUserPass($pass) ? $this->ajaxReturn(false) : $this->ajaxReturn(true);
    }

    private function _chkUserPass($password){
        $pass = md5($password);
        $uid  = $this->member_info['uid'];
        $map['member_id'] = $uid;
        $map['member_password'] = $pass;
        $result = M('Member')->where($map)->find();
        return empty($result) ? false : true;
    }

    // 注册 - 发送手机验证码
    public function SendRegCode(){
        $response = [];
        $tel = trim(I('post.tel'));
        if( ! preg_match($this->mobile_preg, $tel) ) {
            $response['code'] = -1;
            $response['msg']  = '您输入的手机号码格式不正确，请检查手机号码';
            $this->ajaxReturn($response);
        }
        if( $this->isExistMobile($tel) ){
            $response['code'] = -2;
            $response['msg']  = '手机号码已被注册';
            $this->ajaxReturn($response); //手机号码已存在
        }

        $content = '您的认证码是： {$code}。请不要把认证码泄露给其他人。如非本人操作，可不用理会！';
        $result  = $this->sendSMSCode($tel, $content);
        $debug = $result['debug'];
        $code  = $result['code'];

        if( $debug && $code == 0 ){
            $response['code'] = 0;        //发送成功，调试模式
            $response['msg']  = $debug;
            $this->ajaxReturn($response);
        }elseif( !$debug && $code == 0 ){
            $response['code'] = 0;
            $response['msg']  = '注册认证码已成功发送到您的手机上，请注意查收';
            $this->ajaxReturn($response);
        }else{
            $response['code'] = -3;
            $response['msg']  = '验证码发送失败，可能是您的操作太频繁，请稍候再试';
            $this->ajaxReturn($response);
        }
    }

    public function SmsCodeVerify(){
        $post = I('post.', '', 'trim');
        $mobile = $post['tel'];
        $code = $post['code'];
        $this->chkSMSCode($mobile, $code, false) ? $this->ajaxReturn(true) : $this->ajaxReturn(false);
    }

    public function GetCategoryOptionsHtml(){
        $pid = (int)I('post.v');
        if( ! $pid > 0 ) exit('');
        $result = D('GoodsCategory')->get($pid);
        $response = '<option value="0">请选择</option>';
        if( !empty($result) ){
            foreach($result as $k => $v){
                $response .= '<option value="'.$v['gc_id'].'">'.$v['gc_name'].'</option>';
            }
        }
        echo $response;
    }

    public function GetTypeOptionsHtml(){
        $cid = (int)I('post.v');
        if( ! $cid > 0 ) exit('');
        $relation = D('GoodsCategory')->field('gc_relation')->getOne($cid)['gc_relation'];
        $tmp = explode(',', $relation);
        $rid = $tmp[1];
        unset($tmp);
        $map['gc_id'] = $rid;
        $result = M('GoodsType')->where($map)->select();
        $response = '<option value="0">请选择</option>';
        if( !empty($result) ){
            foreach($result as $k => $v){
                $response .= '<option value="'.$v['type_id'].'">'.$v['type_name'].'</option>';
            }
        }
        echo $response;
    }

}